(function ($) {
	$.resourceCourse = function () {
		return {
			allData:[],
			attachmentList:[],
			courseWareDTOListSel:[],
			init: function(){
				this.listener();
				this.initLayui();
				this.resourceList();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
				//点击本讲简介
				$("#resourceCourse").on("click",".intro",function(){

					//页面层
					layer.open({
						type: 1,
						title:'本讲简介',
						shadeClose: true, //开启遮罩关闭
						area: ['500px', '300px'], //宽高
						content: '<div style="padding:20px; word-wrap:break-word">'+$.resourceCourse.allData[$(this).data('array')].description+'</div>'
					});
				});
				//点击备选资料
				$("#resourceCourse").on("click",".material",function(){
					$.resourceCourse.attachmentList= $.resourceCourse.allData[$(this).data('array')].attachmentList;
					//页面层
					layer.open({
						type: 2,
						title:'备课资料',
						shadeClose: true, //开启遮罩关闭
						area: ['500px', '300px'], //宽高
						content: 'pageFrame/material.html'
					});
				});
				//点击引用
				$("#resourceCourse").on("click",".outWareImport",function(){
					$.resourceCourse.courseWareDTOListSel=[];
					var courseWareDTOList = $.resourceCourse.allData[$(this).data('array')].courseWareDTOList;
					var inputs=$(this).parents('.item').find(".body input[type='checkbox']:checked");
					if(inputs.length>0){
						$.each(inputs,function(i,inputDom){
							$.resourceCourse.courseWareDTOListSel.push(courseWareDTOList[$(inputDom).data('checkid')]);
						});
						var submitData = {
							unitName:$(this).parents('.head').find('.headName').html(),
							courseWareDTOList:$.resourceCourse.courseWareDTOListSel,
							token:User.token
						};
						//引用完成后input全置为空
						$("input[type='checkbox']").attr("checked",false);
						$.each($("input[type='checkbox']"),function(i,inputs){
							$(inputs).attr("checked",false);
							$.resourceCourse.initLayui();
						});
						updateObject(Doman.url+Doman.outWareImport, true, JSON.stringify(submitData), function (result) {
							if(result.code=="200"){
								layer.msg(result.message, {icon: 1});
							}else{
								layer.msg(result.message, {icon: 2});
							}

						}, function () {
                        });
					}
				});
				//点击课件
				$("#resourceCourse").on("click",".cwpFile",function(){
					//页面层
					// layer.open({
					// 	type: 2,
					// 	title:'备课资料',
					// 	shadeClose: true, //开启遮罩关闭
                     //    area: ['1024px', window.innerHeight - 100 + "px"], //宽高
					// 	content: SystemConfig.openPlayView+$(this).data("fileurl")
					// });
					window.open( SystemConfig.openPlayView+$(this).data("fileurl"))
				});
				//点击题目
				$("#resourceCourse").on("click",".topic",function(){
					if($(this).data("num")>0){
                        $(parent.document).find("#schoolCoursesRight").attr("src","resourceTopic.html?unitid="+$(this).data("unitid"));
                        //对面包屑进行编辑
                        var html='<a data-href="resourceTopic.html?unitid='+$(this).data("unitid")+'">题目详情</a>';
                        $(parent.document).find(".layui-breadcrumb a:last-child").append('<span class="layui-box">&gt;</span>');
                        $(parent.document).find(".layui-breadcrumb").append(html);
						// window.top.xhTop.createNewTab("题目详情", "../../manage/CA102001/resourceTopic.html?unitid="+$(this).data("unitid"));
					}
				});
			},
			initLayui: function(){
				layui.use(['layer','form','table'], function(){
					$ = layui.jquery, layer =(parent.layer === undefined) ? layui.layer : parent.layer, form = layui.form,table = layui.table, laypage = layui.laypage;
					form.render('checkbox');
					//全选
					form.on('checkbox(allChoose)', function(data){
						var child = $(data.elem).parents('.item').find('.body input[type="checkbox"]');
						child.each(function(index, item){
							item.checked = data.elem.checked;
						});
						form.render('checkbox');
					});
            	});
			},
			resourceList: function(){
				var submitData = {
					courseid:getParam("courseId")
				};
                updateObject(Doman.url+Doman.outUnitList, true, JSON.stringify(submitData), function (result) {
					$.resourceCourse.allData=result.unitDTOList;
					var itemHtml='';
					$.each(result.unitDTOList,function(i,data){
						 itemHtml+='<div class="item">'
								+ '    <div class="head">'
								+ '        <div>第'+(i+1)+'讲<span title='+data.unitName+' class="headName">'+data.unitName+'</span></div>'
								+ '        <div class="courseDetail">'
								+ '            <div><span class="intro" data-array='+i+'>本讲简介</span></div>'
								+ '            <div><span class="material" data-array='+i+'>备课资料</span></div>'
								+ '            <div><span class="topic" data-num='+data.topicNum+' data-unitid='+data.unitid+'>共'+data.topicNum+'题</span></div>'
								+ '        </div>'
								+ '        <div>'
								+ '            <input type="checkbox" name="" title="全选" lay-skin="primary" lay-filter="allChoose"  '+(data.courseWareDTOList.length==0?"disabled":"")+'>'
								+ '            <a class="outWareImport '+(data.courseWareDTOList.length==0?"disable":"")+'" data-array='+i+'>引用</a>'
								+ '        </div>'
								+ '     </div>'
								+ '     <div class="body">';
						if(data.courseWareDTOList.length>0){
							var bodyItemHtml='';
							$.each(data.courseWareDTOList,function(i,data){
								bodyItemHtml+='<div class="bodyitem">'
											+ '    <input type="checkbox" name="" lay-skin="primary" data-checkid='+i+'>'
											+ '    <span class="cwpIcon '+(data.fileUrl==""?"":"cwpFile")+'"  data-attachmenturl='+data.attachmentUrl+' data-fileurl='+data.fileUrl+'></span>'
											+ '    <span class="time">'+changeTime(data.timeLength)+'</span>'
											+ '</div>';
							});
							itemHtml+=bodyItemHtml;
						}
						itemHtml+='</div></div>'
					});
					$("#resourceCourse").html(itemHtml);
					$(".headName").wordLimit(20);//截取描述内容
					$.resourceCourse.initLayui();
               }, function () {
                });
			}
		}
	}();
    $.resourceCourse.init();
})(jQuery);